package com.enjoyor.soa.traffic.rest.efoms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.efoms.RepairsInfoDto;
import com.enjoyor.soa.traffic.api.dto.efoms.TemplateDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.IRepairsInfoConsumeService;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/repairs" })
@Api("TemplateController相关api")
@CrossOrigin

public class RepairsInfoController {
	@Autowired
	private IRepairsInfoConsumeService repairsInfoConsumeService;

	@GetMapping(value = { "getRepairsInfoById" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response = TemplateDto.class, code = 0, message = "")
	@ApiOperation("报修单信息查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单ID", paramType = "query", dataType = "String")})
	public ResultPojo getRepairsInfoById(
			@RequestParam(defaultValue = "", value = "repairsId", required = false) String repairsId,
			HttpServletRequest request) {
		return repairsInfoConsumeService.select(repairsId);
	}
	
	@GetMapping(value = { "selectRepairsInfoList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response = RepairsInfoDto.class, code = 0, message = "")
	@ApiOperation("报修单信息查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单ID", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "repStatusCode", value = "报修状态编号", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "devAreaCode", value = "所属区域编号", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "devName", value = "设备名称", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "key", value = "设备名称或设备编号", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "repDeptIds", value = "部门ID", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "press", value = "今日催办", paramType = "query", dataType = "String") })
	public ResultPojo selectRepairsInfoList(
			@RequestParam(defaultValue = "", value = "repairsId", required = false) String repairsId,
			@RequestParam(defaultValue = "", value = "repStatusCode", required = false) String repStatusCode,
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devId", required = false) String devId,
			@RequestParam(defaultValue = "", value = "devName", required = false) String devName,
			@RequestParam(defaultValue = "", value = "key", required = false) String key,
			@RequestParam(defaultValue = "", value = "deviceId", required = false) String deviceId,
			@RequestParam(defaultValue = "", value = "repDeptIds", required = false) String repDeptIds,
			@RequestParam(defaultValue = "", value = "startTime", required = false) String startTime,
			@RequestParam(defaultValue = "", value = "endTime", required = false) String endTime,
			@RequestParam(defaultValue = "", value = "press", required = false) String press,
			HttpServletRequest request) {
		return repairsInfoConsumeService.selectList(repairsId, repStatusCode, devTypeCode, devId, devName, key,press,
				deviceId, repDeptIds, startTime, endTime);
	}

	@SuppressWarnings({ "rawtypes" })
	@GetMapping(value = { "/selectRepairsInfoPage" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单ID", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repStatusCode", value = "报修状态编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devAreaCode", value = "所属区域编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "key", value = "设备名称或设备编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "repDeptIds", value = "部门ID", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "press", value = "今日催办", paramType = "query", dataType = "String")})
	public ResultPojo selectRepairsInfoPage(
			@RequestParam(defaultValue = "", value = "repairsId", required = false) String repairsId,
			@RequestParam(defaultValue = "", value = "repStatusCode", required = false) String repStatusCode,
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devAreaCode", required = false) String devAreaCode,
			@RequestParam(defaultValue = "", value = "key", required = false) String key,
			@RequestParam(defaultValue = "", value = "repDeptIds", required = false) String repDeptIds,
			@RequestParam(defaultValue = "", value = "startTime", required = false) String startTime,
			@RequestParam(defaultValue = "", value = "endTime", required = false) String endTime,
			@RequestParam(defaultValue = "", value = "press", required = false) String press,Page page,
			HttpServletRequest request) {
		return repairsInfoConsumeService.selectPage(page, repairsId, repStatusCode, devTypeCode, devAreaCode,
				key,press, repDeptIds, startTime, endTime);
	}

	@PostMapping(value = { "/createRepairsInfo" }, consumes = { "application/json; charset=UTF-8" }, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("创建报修单")
	public ResultPojo createRepairsInfo(@RequestBody RepairsInfoDto repairsInfoDto, HttpServletRequest request) {
		UserDto userDto = (UserDto) request.getAttribute("currentUser");
		repairsInfoDto.setRepPersonId(userDto.getPersonId());
		repairsInfoDto.setRepPersonName(userDto.getPersonName());
		repairsInfoDto.setRepDeptId(userDto.getOrgId());
		repairsInfoDto.setRepDeptName(userDto.getOrgName());
		return repairsInfoConsumeService.insert(repairsInfoDto, userDto.getPersonName());
	}
	
	@PutMapping(value = { "/updateRepairsInfo" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新报修单")
    public ResultPojo updateRepairsInfo(@RequestBody RepairsInfoDto repairsInfoDto, HttpServletRequest request) {
        return repairsInfoConsumeService.edit(repairsInfoDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }
	
	@DeleteMapping(value = { "/deleteRepairsInfo" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除报修单")
    @ApiImplicitParam(name = "repairsId", value = "报修单编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo deleteRepairsInfo(@RequestParam("repairsId") String repairsId, HttpServletRequest request) {
        return repairsInfoConsumeService.delete(repairsId, ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }
	
	@PutMapping(value = { "/auditRepairsInfo" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("审核")
    @ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "isPass", value = "审核是否通过（1为通过，0为拒绝）", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "operExplain", value = "操作说明", paramType = "query", dataType = "String")
		 })
    public ResultPojo auditRepairsInfo(
    		@RequestParam("repairsId") String repairsId,
    		@RequestParam("isPass") String isPass,
    		@RequestParam("operExplain") String operExplain,
    		HttpServletRequest request) {
        return repairsInfoConsumeService.audit(repairsId, ((UserDto) request.getAttribute("currentUser")).getPersonName(), isPass, operExplain);
    }
	
	@PutMapping(value = { "/cancelRepairsInfo" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("撤销")
    @ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "operExplain", value = "操作说明", paramType = "query", dataType = "String"),
    @ApiImplicitParam(name = "operReasonCode", value = "操作原因CODE", paramType = "query", dataType = "String"),
    @ApiImplicitParam(name = "operReasonName", value = "操作原因NAME", paramType = "query", dataType = "String")
		 })
    public ResultPojo cancelRepairsInfo(
    		@RequestParam("repairsId") String repairsId,
    		@RequestParam("operExplain") String operExplain,
    		@RequestParam("operReasonCode") String operReasonCode,
    		@RequestParam("operReasonName") String operReasonName,
    		HttpServletRequest request) {
        return repairsInfoConsumeService.cancel(repairsId, ((UserDto) request.getAttribute("currentUser")).getPersonName(), operExplain,operReasonCode,operReasonName);
    }
	
	@PutMapping(value = { "/pressRepairsInfo" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("催办报修单")
    @ApiImplicitParams({ @ApiImplicitParam(name = "repairsId", value = "报修单编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "operExplain", value = "操作说明（催办理由）", paramType = "query", dataType = "String")
		 })
    public ResultPojo pressRepairsInfo(
    		@RequestParam("repairsId") String repairsId,
    		@RequestParam("operExplain") String operExplain,
    		HttpServletRequest request) {
        return repairsInfoConsumeService.press(repairsId, ((UserDto) request.getAttribute("currentUser")).getPersonName(), operExplain);
    }
	
	@GetMapping(value = { "devRepeatCheck" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response = RepairsInfoDto.class, code = 0, message = "")
	@ApiOperation("设备报修重复校验")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "devTypeCode", value = "设备类型编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "devId", value = "设备编号", paramType = "query", dataType = "String")})
	public ResultPojo selectDevRepeatCheck(
			@RequestParam(defaultValue = "", value = "devTypeCode", required = false) String devTypeCode,
			@RequestParam(defaultValue = "", value = "devId", required = false) String devId,
			HttpServletRequest request) {
		return repairsInfoConsumeService.devRepeatCheck(devTypeCode, devId);
	}
	
	@GetMapping(value = { "/selectOperationCount" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("获取报修单各状态数量统计")
    @ApiImplicitParams({ 
    	@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String") })
    public ResultPojo selectOperationCount(
    		@RequestParam(defaultValue = "", value = "startTime", required = false) String startTime,
    		@RequestParam(defaultValue = "", value = "endTime", required = false) String endTime,
    		HttpServletRequest request) {
        return repairsInfoConsumeService.getOperationCount(startTime, endTime);
    }
	
}
